package api

import (
	db "fiberAmisAdmin/config"
	"fmt"
	"github.com/gofiber/fiber/v2"
)

type AdminApiController struct {
	BaseController
}

// 获取所有
func (tc *AdminApiController) GetAllMenu(ctx *fiber.Ctx) error {
	var result []map[string]interface{}
	err := db.MySQLDB.Table("admin_menus").Find(&result).Error
	if err != nil {
		fmt.Println("err:", err)
		return tc.returnError(ctx, fiber.StatusOK, "Error")
	}
	return tc.returnSuccess(ctx, fiber.StatusOK, "Success", result)
}

// 获取单个
func (tc *AdminApiController) GetMenu(ctx *fiber.Ctx) error {
	var result []map[string]interface{}
	err := db.MySQLDB.Table("admin_menus").Find(&result).Error
	if err != nil {
		fmt.Println("err:", err)
		return tc.returnError(ctx, fiber.StatusOK, "Error")
	}
	return tc.returnSuccess(ctx, fiber.StatusOK, "Success", result)
}

// 创建
func (tc *AdminApiController) CreateMenu(ctx *fiber.Ctx) error {
	var requestData struct {
		FormData struct {
			Title string `json:"title"`
		} `json:"formData"`
	}

	if err := ctx.BodyParser(&requestData); err != nil {
		return err
	}

	title := requestData.FormData.Title
	fmt.Println("Title:", title)

	return ctx.JSON(fiber.Map{
		"status": "success",
		"data":   title,
	})
}

// 更新
func (tc *AdminApiController) UpdateMenu(ctx *fiber.Ctx) error {
	var result []map[string]interface{}
	err := db.MySQLDB.Table("admin_menus").Find(&result).Error
	if err != nil {
		fmt.Println("err:", err)
		return tc.returnError(ctx, fiber.StatusOK, "Error")
	}
	return tc.returnSuccess(ctx, fiber.StatusOK, "Success", result)
}

// 删除
func (tc *AdminApiController) DeleteMenu(ctx *fiber.Ctx) error {
	var result []map[string]interface{}
	err := db.MySQLDB.Table("admin_menus").Find(&result).Error
	if err != nil {
		fmt.Println("err:", err)
		return tc.returnError(ctx, fiber.StatusOK, "Error")
	}
	return tc.returnSuccess(ctx, fiber.StatusOK, "Success", result)
}
